package com.example.scaletest;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.mylibrary.scaleweihgt.AndroidUtil;

import java.util.List;

import cn.hutool.core.util.StrUtil;


public class SingleChoicePopUpWindow {
    private Context mContext;
    private PopupWindow mPopupWindow;
    private List<ItemSetEntity> mOptions;
    private OnOptionSelectedListener mListener;

    public interface OnOptionSelectedListener {
        void onOptionSelected(ItemSetEntity option);
        void onSaveWeight(int weight);
    }

    public SingleChoicePopUpWindow(Context context, List<ItemSetEntity> options) {
        mContext = context;
        mOptions = options;
        initPopupWindow();
    }

    private void initPopupWindow() {
        View popupView = LayoutInflater.from(mContext).inflate(R.layout.popup_single_choice, null);
        LinearLayout ll_chose_set = popupView.findViewById(R.id.ll_chose_set);
        ll_chose_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });

        LinearLayout ll_save_w = popupView.findViewById(R.id.ll_save_w);
        RecyclerView ry_chose = popupView.findViewById(R.id.ry_chose);
        EditText et_input = popupView.findViewById(R.id.et_input);
        TextView tv_save_w = popupView.findViewById(R.id.tv_save_w);

        if (mOptions==null){
            ll_save_w.setVisibility(View.VISIBLE);
            ry_chose.setVisibility(View.GONE);
            tv_save_w.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (StrUtil.isNotEmpty(et_input.getText().toString())&&Integer.parseInt(et_input.getText().toString())>0) {
                        mListener.onSaveWeight(Integer.parseInt(et_input.getText().toString()));
                    }else {
                        Toast.makeText(mContext, "请输入整数重量", Toast.LENGTH_SHORT).show();
//                        AndroidUtil.toast("请输入整数重量");
                    }
                }
            });
        }else {
            ll_save_w.setVisibility(View.GONE);
            ry_chose.setVisibility(View.VISIBLE);
            ry_chose.setLayoutManager(new LinearLayoutManager(mContext));
            ChoiceAdapter adapter = new ChoiceAdapter(R.layout.item_chose_set,mOptions);
            ry_chose.setAdapter(adapter);
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    mListener.onOptionSelected(mOptions.get(position));
                }
            });

        }





        mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow的背景为半透明
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x80000000));

        mPopupWindow.setOutsideTouchable(true);
    }

    public void setOnOptionSelectedListener(OnOptionSelectedListener listener) {
        mListener = listener;
    }
    public void dismiss() {
        mPopupWindow.dismiss();
    }
    public void show(View anchorView) {


        if (Build.VERSION.SDK_INT < 24) {
            mPopupWindow.showAsDropDown(anchorView);
        } else {
            if (mContext instanceof Activity) {
                if (null != mPopupWindow) {
                    int screenHeight;
                    // 获取屏幕可用高度：真实高度-虚拟按键的高度
                    screenHeight = AndroidUtil.getContentHeight((Activity) mContext);
                    int[] location = new int[2];
                    // 获取控件在屏幕的位置
                    anchorView.getLocationOnScreen(location);
                    // pop最大高度
                    int maxHeight = screenHeight - location[1] - anchorView.getHeight();
                    // pop 有具体的高度值，但是小于anchor下边缘与屏幕底部的距离， 正常显示
                    if (mPopupWindow.getHeight() > 0 && mPopupWindow.getHeight() < maxHeight) {
                        mPopupWindow.showAsDropDown(anchorView);
                    } else {
                        //设置为最大可用高度
                        mPopupWindow.setHeight(maxHeight);
                        mPopupWindow.showAsDropDown(anchorView,0,0);
                    }
                }


            }
        }
    }

}
